package cn.yunhe.dao.impl;

import cn.yunhe.BaseDB;
import cn.yunhe.dao.ILiuYanDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.LiuYan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LiuYanDao extends BaseDB implements ILiuYanDao {
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;

    @Override
    public boolean addLiuYan(LiuYan liuYan) {
        String sql = "insert into tb_liuyan(liu_title,liu_context,c_id,createdate) values(?,?,?,now())";
        Object[] arr = {liuYan.getTitle(), liuYan.getContext(), liuYan.getAdmin().getUid()};
        return updateSql2(sql, arr) > 0;
    }

    @Override//分页显示所有
    public List<LiuYan> getAll(int pageNum, int pageIndex) {
        List<LiuYan> liuYanList = new ArrayList<>();
        try {
            String sql = "select * from tb_liuyan l,tb_admin a where l.c_id=a.c_id order by l.createdate desc limit ?,?";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, pageNum * (pageIndex - 1));
            psmt.setInt(2, pageNum);
            rs = psmt.executeQuery();
            while (rs.next()) {
                LiuYan liuYan = new LiuYan(rs.getInt("liu_id"), rs.getString("liu_title"),
                        rs.getString("liu_context"), new Admin(rs.getInt("c_id"), rs.getString("c_name")), rs.getDate("createdate"));
                liuYanList.add(liuYan);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, psmt, rs);
        }
        return liuYanList;
    }

    @Override//获得总条数
    public int allCount() {
        int count = 0;
        try {
            String sql = "select count(1) from tb_liuyan";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            if (rs.next())
                count = rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, psmt, rs);
        }
        return count;
    }

    @Override//获取留言对象
    public LiuYan getLiuYan(int lid) {
        LiuYan liuYan = null;
        try {
            String sql = "select * from tb_liuyan where liu_id=?";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, lid);
            rs = psmt.executeQuery();
            if (rs.next()){
                liuYan=new LiuYan(rs.getString("liu_title"), rs.getString("liu_context"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return liuYan;
    }

    @Override
    public boolean updateLiuYan(LiuYan liuYan) {
        List<LiuYan> liuYanList=new ArrayList<>();
        String sql="update tb_liuyan set liu_title=?,liu_context=? where liu_id=?";
        Object[] arr={liuYan.getTitle(),liuYan.getContext(),liuYan.getLid()};
        return updateSql2(sql,arr)>0;
    }
}
